package com.atguigu.edu.realtime.dwd.log.app;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.edu.realtime.common.base.BaseApp;
import com.atguigu.edu.realtime.common.bean.UserVideoBean;
import com.atguigu.edu.realtime.common.constant.Constant;
import com.atguigu.edu.realtime.common.function.BeanToJsonStrMapFunction;
import com.atguigu.edu.realtime.common.util.FlinkSinkUtil;
import com.atguigu.edu.realtime.common.util.FlinkSourceUtil;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

public class DwdInteractionVideo extends BaseApp {
    public static void main(String[] args) {
        new DwdInteractionVideo().start(10013,4, Constant.DWD_INTER_VIDEO,Constant.TOPIC_LOG);
    }
    @Override
    public void handle(StreamExecutionEnvironment env, DataStreamSource<String> kafkaStrDS) {
        SingleOutputStreamOperator<JSONObject> jsonDS = kafkaStrDS.process(
                new ProcessFunction<String, JSONObject>() {
                    @Override
                    public void processElement(String s, ProcessFunction<String, JSONObject>.Context context, Collector<JSONObject> collector) throws Exception {
                        JSONObject jsonObj = JSONObject.parseObject(s);
                       if (jsonObj.containsKey("appVideo")){
                           collector.collect(jsonObj);
                       }

                    }
                }
        );
        jsonDS.print();

     //   KeyedStream<JSONObject, String> keyedDS = jsonDS.keyBy(jsonObject -> jsonObject.getJSONObject("common").getString("sid"));
        SingleOutputStreamOperator<UserVideoBean> singleDS = jsonDS.map(
                new MapFunction<JSONObject, UserVideoBean>() {
                    @Override
                    public UserVideoBean map(JSONObject jsonObject) throws Exception {
                        JSONObject common = jsonObject.getJSONObject("common");
                        String uid = common.getString("uid");
                        String sid = common.getString("sid");
                        JSONObject appVideo = jsonObject.getJSONObject("appVideo");
                        String video_id = appVideo.getString("video_id");
                        Long position_sec = appVideo.getLong("play_sec");
                        Long ts = jsonObject.getLong("ts");
                        return new UserVideoBean(uid, sid, video_id,  position_sec, ts);
                    }
                }
        );
       // singleDS.print("a");
        SingleOutputStreamOperator<UserVideoBean> reduceDS = singleDS.keyBy(UserVideoBean::getTs).keyBy(UserVideoBean::getVideo_id).keyBy(UserVideoBean::getUid).reduce(
                new ReduceFunction<UserVideoBean>() {
                    @Override
                    public UserVideoBean reduce(UserVideoBean v1, UserVideoBean v2) throws Exception {
                        v1.setPlay_sec(v1.getPlay_sec() + v2.getPlay_sec());
                        return v1;
                    }
                }
        );

        SingleOutputStreamOperator<String> jsonStrDS = reduceDS.map(new BeanToJsonStrMapFunction<>());
        jsonStrDS.sinkTo(FlinkSinkUtil.getKafkaSink(Constant.DWD_INTER_VIDEO));


        try {
            env.execute();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


    }
}
